# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=opencv
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.4.1
pkgrel=2
pkgdesc="Open Source Computer Vision Library (mingw-w64)"
arch=('any')
url="https://opencv.org/"
license=("BSD")
depends=(#"${MINGW_PACKAGE_PREFIX}-gst-plugins-base
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         #"${MINGW_PACKAGE_PREFIX}-qt5"
         #"${MINGW_PACKAGE_PREFIX}-gtkglext"
         #"${MINGW_PACKAGE_PREFIX}-gtk2"
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             #"${MINGW_PACKAGE_PREFIX}-qt5"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-ffmpeg"
             "${MINGW_PACKAGE_PREFIX}-python2-numpy"
             "${MINGW_PACKAGE_PREFIX}-python3-numpy"
             "${MINGW_PACKAGE_PREFIX}-tesseract-ocr"
             "${MINGW_PACKAGE_PREFIX}-tiny-dnn"
             "${MINGW_PACKAGE_PREFIX}-hdf5"
             #"${MINGW_PACKAGE_PREFIX}-vtk"
             #"${MINGW_PACKAGE_PREFIX}-cereal"
            )
optdepends=("${MINGW_PACKAGE_PREFIX}-eigen3"
            "${MINGW_PACKAGE_PREFIX}-ffmpeg: support to read and write video files"
            "${MINGW_PACKAGE_PREFIX}-python2-numpy: Python 2.x interface"
            "${MINGW_PACKAGE_PREFIX}-python3-numpy: Python 3.x interface"
            "${MINGW_PACKAGE_PREFIX}-hdf5: opencv_hdf module"
            "${MINGW_PACKAGE_PREFIX}-tesseract-ocr: opencv_text module"
            #"${MINGW_PACKAGE_PREFIX}-vtk: opencv_viz module"
            )
source=("${_realname}-${pkgver}.tar.gz"::https://github.com/opencv/opencv/archive/${pkgver}.tar.gz
        "${_realname}_contrib-${pkgver}.tar.gz"::https://github.com/opencv/opencv_contrib/archive/${pkgver}.tar.gz
        '0001-mingw-w64-cmake.patch'
        '0002-solve_deg3-underflow.patch'
        '0003-issue-4107.patch'
        '0004-generate-proper-pkg-config-file.patch'
        '0005-opencv-support-python-3.6.patch'
        '0006-somehow-uint-not-detected.patch'
        '0007-mingw-w64-have-sincos.patch'
        '0008-mingw-w64-cmake-lib-path.patch'
        '0009-openblas-find.patch'
        '0010-find-libpng-header.patch'
        '0011-dshow-build-fix.patch'
        '0012-dnn_modern-test-fix.patch'
        '0013-dnn_modern-NO_STRICT-fix.patch'
        '0100-fix-precomp-usage.patch'
        '0101-avx512-mingw-w64.patch'
        '0102-make-header-usable-with-C-compiler.patch')
sha256sums=('f1b87684d75496a1054405ae3ee0b6573acaf3dad39eaf4f1d66fdd7e03dc852'
            '298c69ee006d7675e1ff9d371ba8b0d9e7e88374bb7ba0f9d0789851d352ec6e'
            '8eddfeb6cd470dd1fe2871cce5e75f15518aa62cc0b02fb09b07f641f90236ce'
            'fd4e095c3c879413184fc6b91a7b0a77dbb128612341a8be2c99d804a203e362'
            '52ebc8875b9ef3ea70897f34509228daeff73d0cab0aa9eb8b931be6a7d32d7f'
            'aaf2a1952b04c9be3a133b08b0a06f536b4e9ba5f312a03f41301dba61a3fc94'
            '837e637d1040b6bc3427d650e26cf1abb0178b322f2fbb43ccb2d5506f7b1c58'
            '7d2ff25f97c84b59793502786dd64e25c8ca991b0523ffea56b45ce031e80c3f'
            'ba227bb88c7e6948c54c5a3c8347daf5e2c930e99fb155047015bc5cfa2b33e5'
            '434b3bb1f6fd3af31952de5c4af95eb726f4e45b077e98195f8c204732bc9754'
            'e3e2cfc85a9588c545c08f12dcdf933b8b32df9b887944b84c01d520f563ac21'
            '15d4cf15196c7557af6e14d1d7b99aa9b462bd13435b3878dc28f85d00aa3e72'
            '7958ac5d80c643060e482fd6bac3bddd1199c1629ea6bea674179e7279b07d5d'
            '44dc368dcf0c882a81fe4a38b4269e7139686cb108890d890c14db3b0ec4c002'
            '01e305d36eaafbde8de20efd6eef13c34feb1bc8e6f9b54102bc20b84dfc9aa8'
            '0fa60dca956a61a5ac78c216994d7304de40ad86ce428e8a0cdf2ba69df054b6'
            '5d110b537eea55a1dfa9934277a8a7d9d3730d6243408e31c5ebdfb87d8b0373'
            '7350e41ac9dfd95213b880ff017edda155ad704e35e024cfd9f42d286178b1fb')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}/0001-mingw-w64-cmake.patch"
  patch -Np1 -i "${srcdir}/0002-solve_deg3-underflow.patch"
  patch -Np1 -i "${srcdir}/0003-issue-4107.patch"
  patch -Np1 -i "${srcdir}/0004-generate-proper-pkg-config-file.patch"
  patch -Np1 -i "${srcdir}/0005-opencv-support-python-3.6.patch"

  patch -Np1 -i "${srcdir}/0008-mingw-w64-cmake-lib-path.patch"
  patch -Np1 -i "${srcdir}/0009-openblas-find.patch"
  patch -Np1 -i "${srcdir}/0010-find-libpng-header.patch"
  patch -Np1 -i "${srcdir}/0011-dshow-build-fix.patch"
  
  # Upstream patches
  patch -Np1 -i "${srcdir}/0100-fix-precomp-usage.patch"
  patch -Np1 -i "${srcdir}/0101-avx512-mingw-w64.patch"
  
  patch -Np1 -i "${srcdir}/0102-make-header-usable-with-C-compiler.patch"
  
  cd "${srcdir}/${_realname}_contrib-${pkgver}"
  patch -Np1 -i "${srcdir}/0006-somehow-uint-not-detected.patch"
  patch -Np1 -i "${srcdir}/0007-mingw-w64-have-sincos.patch"
  patch -Np1 -i "${srcdir}/0012-dnn_modern-test-fix.patch"
  patch -Np1 -i "${srcdir}/0013-dnn_modern-NO_STRICT-fix.patch"
}

build() {
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}
  
  export OpenBLAS_HOME=${MINGW_PREFIX}
  export TINYDNN_ROOT=${MINGW_PREFIX}/include/tiny_dnn

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DPYTHON2_PACKAGES_PATH=;-DPYTHON3_PACKAGES_PATH=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DPKG_CONFIG_WITHOUT_PREFIX=ON \
    -DWITH_OPENCL=ON \
    -DWITH_OPENGL=ON \
    -DWITH_TBB=ON \
    -DWITH_XINE=OFF \
    -DBUILD_WITH_DEBUG_INFO=OFF \
    -DBUILD_DOCS=OFF \
    -DBUILD_TESTS=OFF \
    -DBUILD_PERF_TESTS=OFF \
    -DBUILD_EXAMPLES=ON \
    -DINSTALL_C_EXAMPLES=OFF \
    -DINSTALL_PYTHON_EXAMPLES=OFF \
    -DWITH_GTK=OFF \
    -DWITH_QT=OFF \
    -DWITH_VTK=OFF \
    -DWITH_GDAL=OFF \
    -DWITH_FFMPEG=ON \
    -DWITH_GSTREAMER=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_SKIP_RPATH=ON \
    -DENABLE_PRECOMPILED_HEADERS=OFF \
    -DOPENCV_EXTRA_MODULES_PATH=../${_realname}_contrib-${pkgver}/modules \
    -DBUILD_opencv_python2=ON \
    -DBUILD_opencv_python3=ON \
    -DPYTHON2_EXECUTABLE=${MINGW_PREFIX}/bin/python2 \
    -DPYTHON3_EXECUTABLE=${MINGW_PREFIX}/bin/python3 \
    -DPYTHON2_PACKAGES_PATH=$(cygpath -u $(python2 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")) \
    -DPYTHON3_PACKAGES_PATH=$(cygpath -u $(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")) \
    -DBUILD_opencv_bioinspired=OFF \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR=${pkgdir} install
}
